package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseFragment;
import com.isunland.managesystem.entity.BaseParams;
import com.isunland.managesystem.utils.ToastUtil;

/**
 * 对象功能: 基本查询类
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: wangyanan
 * 创建时间: 2016/12/21 19:02
 */

public abstract class BaseQueryFragment extends BaseFragment {

  /**
   * 返回的
   */
  //public static final String EXTRA_RESULT_START_DATE = "com.isunland.intelligentFarmbyWJ.ui.QueryDateFragment.EXTRA_RESULT_START_DATE";
  //public static final String EXTRA_RESULT_END_DATE = "com.isunland.intelligentFarmbyWJ.ui.QueryDateFragment.EXTRA_RESULT_END_DATE";

  private static final int MENU_ITEM_CLEAR = 4;
  private static final int MENU_ITEM_CONFIRM = 5;

  @Override public void initData() {
    super.initData();
    //FragmentManager mFm = mActivity.getSupportFragmentManager();
  }

  @Override public void initView(View view, Bundle savedInstanceState) {
    super.initView(view, savedInstanceState);
    setTitleCustom(R.string.query);
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

    menu.add(0, MENU_ITEM_CLEAR, 0, R.string.clearChoose)
        .setIcon(R.drawable.delete)
        .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);

    menu.add(0, MENU_ITEM_CONFIRM, 1, R.string.confirm)
        .setIcon(R.drawable.confirm)
        .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);

    super.onCreateOptionsMenu(menu, inflater);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {

      case MENU_ITEM_CONFIRM:
        BaseParams mParams = confirm();
        if (mParams == null) return false;
        Intent intent = new Intent();
        intent.putExtra(EXTRA_PARAMS, mParams);
        mActivity.setResult(Activity.RESULT_OK, intent);
        if (!mActivity.isFinishing()) mActivity.finish();
        return true;
      case MENU_ITEM_CLEAR:
        clear();
        ToastUtil.show(R.string.clearChooseCondition);
        break;
      default:
        break;
    }

    return super.onOptionsItemSelected(item);
  }

  protected abstract BaseParams confirm();

  /** 清空页面及数据 */
  protected abstract void clear();
}